import MsgProto from './MsgProto_pb'
import {guid} from "../util/guid";

/**
 * 消息的一系列操作
 */
class MsgCenter {

    static MsgType = {
        'TEXT': 0,
        'IMAGE': 1,
        'VOICE': 2,
        'MEDIA': 3,
        'CONNECT': 4,
        'KEEPALIVE': 5,
        'FRIEND': 6,
        'SIGNED': 7
    }

    static chatType = {
        'SINGLE':0,
        'GROUP':1
    }


    /**
     * 设置当前正在聊天的对象
     * @param fromUser
     * @param toUser
     * @param chatType
     */
    static initChatUser(fromUserId,toUserId,nowChatType){
        this.fromUserId = fromUserId
        this.toUserId = toUserId
        this.nowChatType = nowChatType
    }

    /**
     * 创建Proto类型消息实体
     * @param content
     * @param type
     * @param chatType
     * @param localId
     * @param serverId
     * @returns {MsgProto.MsgEntity}
     */
    static createMsgProto (content, type, localId, serverId) {

        let msg = new MsgProto.MsgEntity();
        msg.setGuid(guid())//设置唯一ID
        if(this.fromUserId) {
            msg.setFromuserid(this.fromUserId)
        }
        if(this.toUserId){
            msg.setTouserid(this.toUserId)
        }
        msg.setType(type)
        msg.setChattype(this.nowChatType);
        msg.setContent(content)
        let time = new Date().getTime()
        console.log(time)
        msg.setCreatetime(time)
        if (localId || serverId) {
            msg.setLocalid(localId)
            msg.setServerid(serverId)
        }
        //自己发送
        // msg.setIssend(true);
        return msg;
    }

    //创建文本消息
    static  createTextMsgProto (content) {
        let textMsg = this.createMsgProto(content, this.MsgType.TEXT)
        // MyWebSocket.ws.send(MsgCenter.serializeBinary(textMsg))
        return textMsg
    }

    //创建图片消息
    static createImageMsgProto(localId,serverId) {
        let imageMsg =  this.createMsgProto('图片信息', this.MsgType.IMAGE,localId,serverId)
        return imageMsg
    }

    //创建语音消息
    static createVoiceMsg (recordTime,localId,serverId)  {
        let content = {time: recordTime, translate: '翻译内容'}
        let voiceMsg = this.createMsgProto(JSON.stringify(content), this.MsgType.VOICE, localId, serverId)
        return voiceMsg;
    }

    static createHeartMsg ()  {
        let heartMsg = this.createMsgProto('我还活着', this.MsgType.KEEPALIVE)
        return heartMsg
    }

    static createConnectMsg () {
        let connenctMsg = this.createMsgProto('连接消息', this.MsgType.CONNECT)
        return connenctMsg;
    }

    //将字节转换成MsgProto对象
    static deserializeBinary  (bytes)  {
        return MsgProto.MsgEntity.deserializeBinary(bytes)
    }

    //将Msg对象转成字节
    static serializeBinary  (msg)  {
        return msg.serializeBinary()
    }

    /**
     * 获取这条消息与当前用户聊天的用户Id
     * @param msgProto
     * @param userId
     */
    static getChatUserId  (msgObject,userId)  {
        //群聊
        if(msgObject.chattype==this.chatType.GROUP){

            return msgObject.touserid
        }else { //单聊
            if(msgObject.fromuserid == userId){
                return msgObject.touserid
            }else {
                return msgObject.fromuserid
            }
        }
    }




}

export default MsgCenter